/**
 * 
 */
package edu.unr.cater.npap.server.security;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;

/**
 * @author Hao
 * 
 */
public class UserAuthListener implements
		ApplicationListener<AbstractAuthenticationEvent> {

	private static final Log logger = LogFactory.getLog(UserAuthListener.class);

	@Override
	public void onApplicationEvent(AbstractAuthenticationEvent arg0) {
		// TODO Auto-generated method stub
		final StringBuilder builder = new StringBuilder();
		builder.append("Authentication event ");
		builder.append(arg0.getClass().getSimpleName());
		builder.append(": ");
		builder.append(arg0.getAuthentication().getName());
		builder.append("; details: ");
		builder.append(arg0.getAuthentication().getDetails());
		if (arg0 instanceof AbstractAuthenticationFailureEvent) {
			builder.append("; exception: ");
			builder.append(((AbstractAuthenticationFailureEvent) arg0)
					.getException().getMessage());
		}

		logger.warn(builder.toString());
	}

}
